package auto.app.service.impl;

import auto.app.controller.from.AutoGlobal.AutoGlobalAddFrom;
import auto.app.controller.from.AutoGlobal.AutoGlobalDeleteFrom;
import auto.app.controller.from.AutoGlobal.AutoGlobalPageFrom;
import auto.app.controller.from.AutoGlobal.AutoGlobalUpdateFrom;
import auto.app.controller.info.AutoGlobal.AutoGlobalDetailInfo;
import auto.app.controller.info.AutoGlobal.AutoGlobalPageInfo;
import auto.app.exetion.MainExection;
import auto.app.model.AutoGlobal;
import auto.app.dao.AutoGlobalDao;
import auto.app.service.AutoGlobalService;
import auto.app.util.CommonUtil;
import auto.app.util.ObjectNull;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.validation.constraints.NotNull;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.function.Consumer;

import static auto.app.exetion.MainExetionMessage.AUTOGLOBALNAMEERROR;
import static auto.app.exetion.MainExetionMessage.AUTOGLOBALSELECTERROR;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 杨森
 * @since 2022-08-18
 */
@Service
@Slf4j
public class AutoGlobalServiceImpl extends ServiceImpl<AutoGlobalDao, AutoGlobal> implements AutoGlobalService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean addAutoGlobal(AutoGlobalAddFrom autoGlobalAddFrom) throws MainExection {
        //校验参数名是否重复
        LambdaQueryWrapper<AutoGlobal> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(AutoGlobal::getParameName, autoGlobalAddFrom.getParameName());
        List<AutoGlobal> autoGlobals = baseMapper.selectList(lambdaQueryWrapper);
        if (autoGlobals.size()>0 && ObjectUtil.isNotNull(autoGlobals)) {
            throw new MainExection(AUTOGLOBALNAMEERROR.getCode(), AUTOGLOBALNAMEERROR.getMsg());
        }
        //新增参数
        AutoGlobal autoGlobal1 = CommonUtil.copyOne(autoGlobalAddFrom, AutoGlobal.class);
        autoGlobal1.setCreateTime(LocalDateTime.now());
        baseMapper.insert(autoGlobal1);
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateAutoGlobal(AutoGlobalUpdateFrom autoGlobalUpdateFrom) throws MainExection {
        //校验全局参数是否存在
        AutoGlobal autoGlobalSelect = baseMapper.selectById(autoGlobalUpdateFrom.getGlobalId());
        if (autoGlobalSelect == null) {
            throw new MainExection(AUTOGLOBALSELECTERROR.getCode(), AUTOGLOBALSELECTERROR.getMsg());
        }
        //校验参数名是否重复
        LambdaQueryWrapper<AutoGlobal> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(AutoGlobal::getParameName, autoGlobalUpdateFrom.getParameName())
        .ne(AutoGlobal::getGlobalId,autoGlobalUpdateFrom.getGlobalId());
        AutoGlobal autoGlobal = baseMapper.selectOne(lambdaQueryWrapper);
        if (ObjectNull.isNotNull(autoGlobal)) {
            throw new MainExection(AUTOGLOBALNAMEERROR.getCode(), AUTOGLOBALNAMEERROR.getMsg());
        }
        //修改
        AutoGlobal autoGlobalUpdate = CommonUtil.copyOne(autoGlobalUpdateFrom, AutoGlobal.class);
        autoGlobalUpdate.setLastupdateTime(LocalDateTime.now());
        baseMapper.updateById(autoGlobalUpdate);
        return true;
    }

    @Override
    public AutoGlobalDetailInfo autoGlobalDetail(String globalId) throws MainExection {
        //校验全局参数是否存在
        AutoGlobal autoGlobalSelect = baseMapper.selectById(globalId);
        if (autoGlobalSelect == null) {
            throw new MainExection(AUTOGLOBALSELECTERROR.getCode(), AUTOGLOBALSELECTERROR.getMsg());
        }
        return CommonUtil.copyOne(autoGlobalSelect, AutoGlobalDetailInfo.class);
    }

    @Override
    public AutoGlobalPageInfo autoGlobalList(AutoGlobalPageFrom autoGlobalPageFrom) {
        LambdaQueryWrapper<AutoGlobal> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.orderByDesc(AutoGlobal::getCreateTime);
        AutoGlobalPageFrom autoGlobalPageFrom1 = baseMapper.selectPage(autoGlobalPageFrom, lambdaQueryWrapper);
        return CommonUtil.copyOne(autoGlobalPageFrom1, AutoGlobalPageInfo.class);
    }

    @Override
    public List<AutoGlobal> autoGlobalListCase() {
        //查询状态为启用的返回
        LambdaQueryWrapper<AutoGlobal> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(AutoGlobal::getGlobalState, 0);
        return baseMapper.selectList(lambdaQueryWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteGlobalList(AutoGlobalDeleteFrom autoGlobalDeleteFrom) throws MainExection {
        //查询全局参数是否存在
        List<String> globalIdList = autoGlobalDeleteFrom.getGlobalIdList();
        List<AutoGlobal> autoGlobals = baseMapper.selectBatchIds(globalIdList);
        HashMap<String, String> mapGlobal = new HashMap<>();
        for (String integer : globalIdList) {
            mapGlobal.put(integer, "删除全局参数id为:" + integer);
        }
        for (AutoGlobal autoGlobal : autoGlobals) {
                String s = mapGlobal.get(autoGlobal.getGlobalId());
                if (ObjectNull.isNull(s)){
                    throw new MainExection(AUTOGLOBALSELECTERROR.getCode(), AUTOGLOBALSELECTERROR.getMsg());
                }
        }
        int i = baseMapper.deleteBatchIds(globalIdList);
        return true;
    }
}
